package com.omi.iberolactalpreventa;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class OpcionesRed extends Activity{
	
	private Toast tCambiosOk;
	private EditText txtFtpDir;
	private EditText txtFtpuser;
	private EditText txtFtpPassword;
	private EditText txtPortFtp;
	private CheckBox chkFtpPasivo;
	
	@SuppressLint("NewApi")
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_opciones_red);
		getActionBar().setDisplayHomeAsUpEnabled(true);
		
		txtFtpDir = (EditText) findViewById(R.id.txtFtpDir);
		txtFtpuser = (EditText) findViewById(R.id.txtFtpuser);
		txtFtpPassword = (EditText) findViewById(R.id.txtFtpPassword);
		txtPortFtp = (EditText) findViewById(R.id.txtPortFtp);
		chkFtpPasivo = (CheckBox) findViewById(R.id.chkFtpPasivo);
		
		//Al entrar, lo primero que hacemos es cargar los textbox exceptuando el de la contraseña:
		Cursor c = Modulebase.db.ejecutarSelect(new String[]{"FTP_SERVER","FTP_USER","FTP_PASIVE","PORT"}, "CONFIGURACION", null, null);
		
		if(c!=null && c.moveToFirst() && c.getString(0)!=null)
		{

			txtFtpDir.setText(c.getString(0));
			txtFtpuser.setText(c.getString(1));
			txtPortFtp.setText(c.getString(3));
			
			if(c.getString(2).equals("1"))
			{
				chkFtpPasivo.setChecked(true);
			}
			else
			{
				chkFtpPasivo.setChecked(false);
			}
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.credenciales,menu);
		return true;
	}
	
	 @Override
     public boolean onOptionsItemSelected(MenuItem item) {
             switch (item.getItemId()) {
             case android.R.id.home:
              finish();
              break;
             case R.id.btnMenuAceptar:
     	    	//Guardamos los datos de los cambios:
            	 String consulta;
            	 int ftpPasive;
            	 if(chkFtpPasivo.isChecked())
            	 {
            		 ftpPasive = 1;
            	 }
            	 else
            	 {
            		 ftpPasive = 0;
            	 }
            	 if(!txtFtpPassword.getText().equals(""))
            	 {
            		 consulta = "UPDATE CONFIGURACION SET FTP_SERVER='"+txtFtpDir.getText()+"',FTP_USER='"+txtFtpuser.getText()+"',FTP_PASIVE='"+ftpPasive+"',PORT='"+txtPortFtp.getText()+"',FTP_PASSWORD='"+txtFtpPassword.getText()+"' WHERE USUARIO='ADMIN'";
            	 }
            	 else
            	 {
            		 consulta = "UPDATE CONFIGURACION SET FTP_SERVER='"+txtFtpDir.getText()+"',FTP_USER='"+txtFtpuser.getText()+"',FTP_PASIVE='"+ftpPasive+"',PORT='"+txtPortFtp.getText()+"' WHERE USUARIO='ADMIN'";
            	 }
     	    	if(Modulebase.db.ejecutarNonQuery(consulta))
     	    	{
     	    		tCambiosOk = Toast.makeText(this,"Cambios guardados correctamente", Toast.LENGTH_SHORT);
     	    		tCambiosOk.show();
     	    		finish();
     	    		return true;
     	    	}
     	    	tCambiosOk = Toast.makeText(this,"Error al modificar los datos", Toast.LENGTH_SHORT);
         		tCambiosOk.show();
     	    	return false;
 
             default: 
              return super.onOptionsItemSelected(item);      
             }
             return false;
     }
	 
	 @Override
	 public void onBackPressed() {
	 	finish();
	 }
}
